package com.itheima.sh;

import com.itheima.sh.config.MyConfig;
import com.itheima.sh.config.MyImportBeanDefinitionRegistrar;
import com.itheima.sh.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@SpringBootApplication
//1、 User 创建Bean存到 IOC容器， name=类名全路径 com.itheima.sh.pojo.User
//@Import(User.class)

//2、导入配置类：MyConfig Bean 存到IOC容器 name=MyConfig类名全路径, User Bean存在  name=方法名
//@Import(MyConfig.class)

//3、导入ImportSelector选择器，按照返回的数组类创建Bean， name=类的全路径, @bean 创建Bean name=默认是方法名
// 批量导入Bean对象 ***
//@Import(MyImportSelector.class)

//4、导入ImportBeanDefinitionRegistrar实现类， 按照registerBeanDefinitions方法注入Bean对象
@Import(MyImportBeanDefinitionRegistrar.class)
public class AutoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(AutoApplication.class, args);

     /*   // 证明 容器中 有两个Bean
//        User user = applicationContext.getBean(User.class);
        Object user1 = applicationContext.getBean("user");
        Object user2 = applicationContext.getBean("user");
        System.out.println(user1 == user2);
        MyConfig myConfig = applicationContext.getBean(MyConfig.class);
//        Object myConfig1 = applicationContext.getBean("MyConfig");
//        Object myConfig2 = applicationContext.getBean("MYConfig");
//        System.out.println(myConfig1 == myConfig2);

        User user = myConfig.getUser();
        System.out.println(user1 == user);  // true*/


       // @Import
//        User user = applicationContext.getBean(User.class);
//        Object user = applicationContext.getBean("com.itheima.sh.pojo.User");
//        System.out.println(user);  // 单例

        Map<String, User> userMap = applicationContext.getBeansOfType(User.class);
        System.out.println(userMap);
        Map<String, MyConfig> myConfigMap = applicationContext.getBeansOfType(MyConfig.class);
        System.out.println(myConfigMap);
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(AutoApplication.class);
    }
}
